/*
Released under this MIT License:
	
Copyright (c) 2010 Bart van der Schoor
	
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
	
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
	
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
	
 */
package bruce
{
	/**
	 *	Bruce Simple AS3 Banner Toolkit
	 *	<pre>
	 *	http://code.google.com/p/bruce-btk/
	 *	standard stuff minimized
	 *	status: brutally refactored to readyish
	 *	</pre>	
	 * @author Bart van der Schoor at google's email
	 * @version 0.8
	 */
	public class Bruce
	{
		// mute log output
		public static var enabled:Boolean = true;
		// throw errors or log
		public static var errors:Boolean = false;
		// reference to any single argument logger
		public static var output:Function = trace;
		// aboutish
		public static const version:String = '0.8';
		// shared string
		public static const eNull:String = ' argument is null';
		// shared string
		public static const eclNull:String = ' clip ' + eNull;
		// shared string
		public static const eEmpty:String = eNull + ' or empty';

		/**
		 * 	send some text or object to the log output like trace. you can disable all output by Bruce.enable or set custom logger callback on Bruce.output
		 *	@param ...args any object to log
		 *	@return same clip
		 *	@example Bruce.log(myButton);
		 *	@author Bart van der Schoor / Bruce
		 *	@version 0.8
		 */
		public static function log(...args):void
		{
			if (Bruce.enabled && Bruce.output !== null)
			{
				// array-like objects are evil
				Bruce.output.apply(null, [String(args)]);
			}
		}

		/**
		 * 	easy send standard ArgumentError error using Bruce.error
		 *	@param error argument message
		 *	@param ret return value (null)
		 *  @return ret argument
		 *	@example Bruce.arg('missing item in '+myInput);
		 *	@author Bart van der Schoor / Bruce
		 *	@version 0.8
		 */
		public static function arg(error:String, ret:*=null):*
		{
			// error = error ? error : 'Log.arg: error' + eEmpty;
			return Bruce.error(new ArgumentError('\n\n\t-> ' + error + '\n'), ret);
		}
		/**
		 * 	throw some text or error to throw, hard if Bruce.errors is enabled, otherwise send to log with stack if possible (for easy debugging)
		 *	@param error real Error or a message
		 *	@param ret return value (null)
		 *  @return ret argument 
		 *	@example Bruce.error('stuff is messed up!');
		 *	@example Bruce.error(new Error('programmer malfunction'));
		 *	@author Bart van der Schoor / Bruce
		 *	@version 0.8
		 */
		public static function error(error:*, ret:*=null):*
		{
			var e:Error = error is Error ? Error(error) : new Error('\n\n' + error + '\n\n');
			if (Bruce.errors)
			{
				throw(e);
			}
			var stack:String = e.getStackTrace();
			Bruce.log(stack ? (stack + '\n') : ('Log.error: ' + error));
			return ret;
		}
	}
}
